import { StatusBarAlignment, window, workspace, type ExtensionContext, type StatusBarItem } from 'vscode';

let statusBarItems: StatusBarItem[] = []; // 存储状态栏项的引用

// 注册状态栏项的函数
export function registerStatusBarItems(context: ExtensionContext) {
  // 获取用户设置
  const config = workspace.getConfiguration('proiview');
  const showStatusBarItem = config.get<boolean>('showStatusBarItem', true);
  console.log('showStatusBarItem', showStatusBarItem)

  // 移除现有状态栏项
  statusBarItems.forEach(item => {
    item.hide();
    item.dispose();
  });
  statusBarItems = [];


  const statusCommandList = [
    {
      name: 'Proiview文档',
      priority: 0,
      command: 'proiview.open-documentation',
      tooltip: '打开Proiview文档',
    },
  ]

  if (showStatusBarItem) {
    statusCommandList.forEach((item) => {
      const statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, item.priority);
      statusBarItem.command = item.command;
      statusBarItem.text = item.name;
      statusBarItem.tooltip = item.tooltip;
      statusBarItem.show();

      statusBarItems.push(statusBarItem);

      context.subscriptions.push(statusBarItem);
    });
  }
}

// 监听配置更改事件
export function dynamicRegister(context: ExtensionContext) {
  workspace.onDidChangeConfiguration((event) => {
    console.log('event', event, event.affectsConfiguration('proiview.showStatusBarItem'))
    if (event.affectsConfiguration('proiview.showStatusBarItem')) {

      registerStatusBarItems(context);
    }
  });
}
